<template>
  <!-- $route.params.type 获取路由上的参数type -->
  <div>
    <div class="header">
        <router-link tag="span" to="/">&lt;</router-link>
        <h3>更多歌单</h3>
    </div>
    <div class="list clearfix">
        <router-link 
            class="item" 
            :to="`/details/${item.id}`"
            v-for="(item,index) in moreDatas" 
            :key="index"
            :class="{'clear-padding' : index%3 !== 1}"
        >
            <div class="img-warpper">
                <img :src="item.picUrl">
            </div>
            <div class="main">{{cutString(item.name)}}</div>
        </router-link>
    </div>
    <div ref="loadmore">加载更多</div>
  </div>
</template>

<script>
export default {
    name:"MusicMore",
    data(){
        return{
            moreDatas:[]
        }
    },
    mounted(){
        this.http();

        var timer = null;
        // 触底加载更多的事件
        const winHeight = document.documentElement.clientHeight;  //文档高度
        window.addEventListener("scroll", () => {
            if(this.$refs.loadmore){
                let currentHeight = this.$refs.loadmore.getBoundingClientRect().top;
                if(timer){
                    clearTimeout(timer);
                }
                timer = setTimeout(() => {
                    if(winHeight > currentHeight){
                        //网络请求，获取新的数据
                        this.http();
                    }
                }, 300)
            }
        })
    },
    methods:{
        http(){
            this.$api.getRecommend({limit:30}).then(res => {
                // console.log(res);
                // this.moreDatas = res.result;
                if (res.result) {
                    this.moreDatas.push(...res.result);  //上拉加载、下拉合并数据
                }else{
                    this.$refs.loadmore.innerHTML = "别拽了，没有数据了";
                }

            })
        },
        //让文字显示在一行上，超出则打点
        cutString(str){
            if(str.length > 8){
                return str.substring(0, 8) + "..."
            }
            return str;
        }
    }
}
</script>

<style scoped>
.header{
    width: 100%;
    background-color: #fff;
    text-align: center;
    overflow: hidden;
    clear: both;
}
.header span{
    display: block;
    float: left;
    line-height: 40px;
}
.header h3{
    display: inline-block;
    line-height: 40px;
}
.list {
    width: 100%;
}

.list .item {
    float: left;
    width: 31.33%;
    padding: 0 10px;
    margin-bottom: 10px;
}

.clear-padding {
    padding: 0 !important;
}

.main {
    margin-top: 4px;
    font-size: 12px;
}

.gray {
    font-size: 12px;
    color: #999;
}

.btn-more {
    font-size: 12px;
    text-align: right;
    color: #333;
}
</style>